package com.andy.dialog


import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager

/**
 * Author:Andy
 * Date:2023/3/24 18:46
 * Description:BaseDialog
 */
abstract class BaseDialog : DialogFragment() {

    private val mViewSparseArray = SparseArray<View>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.DefaultDialogStyle)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (getLayoutId() != 0) {
            initStyle()
            val view: View = inflater.inflate(getLayoutId(), container, false)
            initView(view)
            return view
        }
        return container
    }

    private var mView: View? = null
    private fun initView(view: View) {
        mView = view
    }


    //获取View视图
    fun <V> findView(id: Int): View {
        var view = mViewSparseArray[id]
        if (view == null) {
            view = mView?.findViewById(id)
            mViewSparseArray.put(id, view)
        }
        return view
    }

    //获取当前View视图
    fun getDialogView(): View {
        return mView!!
    }


    override fun onResume() {
        //去除左右边距 和背景 然后需要自己来定义
        val win = dialog!!.window!!
        dialog!!.window!!.decorView.setPadding(0, 0, 0, 0)
        val lp = win.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        win.attributes = lp
        win.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        super.onResume()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData()
    }


    //初始化数据
    abstract fun initData()

    //初始化样式
    abstract fun initStyle()

    //传递的视图
    abstract fun getLayoutId(): Int


    //点击空白不消失
    fun setDialogCancelable(isCancelable: Boolean) {
        setCancelable(isCancelable)
    }

    //显示dialog
    fun showDialog(manger: FragmentManager) {
        val transaction = manger.beginTransaction()
        val dialog = manger.findFragmentByTag("dialog")
        if (dialog != null) {
            transaction.remove(dialog)
        }
        transaction.addToBackStack(null)
        show(transaction, "dialog")
    }

    //隐藏dialog
    fun hintDialog(manger: FragmentManager) {
        val prev = manger.findFragmentByTag("dialog")
        if (prev != null) {
            (prev as DialogFragment).dismiss()
        }
    }
}